package com.sunda.spmsweb.wmscontroller;


import com.alibaba.fastjson.JSONObject;
import com.sunda.spmscommon.ResponseResult;
import com.sunda.spmswms.entity.WhsStorage;
import com.sunda.spmswms.service.IWhsStorageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 仓库储位信息表 前端控制器
 * </p>
 *
 * @author Wayne
 * @since 2021-03-18
 */
@RestController
@RequestMapping("/whsStorage")
@Api(tags = "仓库储位信息表", description = "仓库储位信息接口")
public class WhsStorageController {

    @Autowired
    IWhsStorageService iWhsStorageService;


    @RequestMapping("/getWhsStorageList")
    @ApiOperation(value = "获取全部仓库储位信息",notes = "获取全部仓库储位信息 \n" +
            "{\n" +
            "\t\"whsCode\": [\"2134\", \"1061\"],\n" +
            "\t\"werks\": [\"CN01\"]\n" +
            "}", httpMethod = "POST")
    public ResponseResult getWhsStorageList(@RequestBody JSONObject jsonObject){

        try {
            List<String> whsCode=jsonObject.containsKey("whsCode")? (List)jsonObject.getJSONArray("whsCode") :null;
            List<String> werks=jsonObject.containsKey("werks")? (List)jsonObject.getJSONArray("werks") :null;

            if(whsCode==null || werks==null ) {
                throw new Exception("仓库代码或是工厂代码为空");
            }

            return ResponseResult.success().add("whsStorageList", iWhsStorageService.getAllWhsStorageList(whsCode,werks));
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("获取仓库信息储位出错");
        }
    }

    @RequestMapping("/getWhsStorageListAll")
    @ApiOperation(value = "获取全部仓库储位信息连带已经删除的储位",notes = "获取全部仓库储位信息连带已经删除的储位 \n" +
            "{\n" +
            "\t\"whsCode\": [\"2134\", \"1061\"],\n" +
            "\t\"werks\": [\"CN01\"]\n" +
            "}", httpMethod = "POST")
    public ResponseResult getWhsStorageListAll(@RequestBody JSONObject jsonObject){

        try {
            List<String> whsCode=jsonObject.containsKey("whsCode")? (List)jsonObject.getJSONArray("whsCode") :null;
            List<String> werks=jsonObject.containsKey("werks")? (List)jsonObject.getJSONArray("werks") :null;

            if(whsCode==null || werks==null ) {
                throw new Exception("仓库代码或是工厂代码为空");
            }

            return ResponseResult.success().add("whsStorageList", iWhsStorageService.getWhsStorageListAll(whsCode,werks));
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("获取仓库信息储位出错");
        }
    }

    @RequestMapping("/createWhsStorage")
    @ApiOperation(value = "新建仓库储位",notes = "新建仓库储位", httpMethod = "POST")
    public ResponseResult createWhsStorage(@RequestBody WhsStorage whsStorage){

        return iWhsStorageService.createWhsStorage(whsStorage);
    }

}
